package com.woniu.atms.controller.aft;


import com.woniu.atms.entity.G5CarType;
import com.woniu.atms.service.CarTypeService;
import com.woniu.atms.util.Code;
import com.woniu.atms.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author lhy
 * @title: AftCarTypeController车辆类型管理
 * @projectName g5_atms
 * @description: TODO
 * @date 2022/1/17 15:18
 */
@RestController
@RequestMapping("/api/aft/v1/car_type")
public class AftCarTypeController {
    //类型列表(/list);车辆信息(/info);添加车辆(/add);删除车辆(/delete)

    @Autowired
    private CarTypeService carTypeService;

    @GetMapping("/list")
    public ResponseResult getList() {
        return new ResponseResult(carTypeService.findAllCarType());
    }

    @PostMapping("/info")
    public ResponseResult getCarTypeById(@RequestBody G5CarType carType) {
        return new ResponseResult(carTypeService.findCarTypeById(carType.getId()));
    }

    @PostMapping("/add")
    public ResponseResult addCarType(@RequestBody @Valid G5CarType carType) {
        //判断车辆类型名是否重复
        String carName = carType.getName();
        G5CarType carType1 = carTypeService.findCarTypeByName(carName);
        if (carType1 != null) {
            return new ResponseResult(Code.FAIL, "车辆类型名已存在！", null);
        }
        boolean flag = carTypeService.addCarType(carType);
        if (flag) {
            return new ResponseResult(Code.SUCCESS, "添加成功！", null);
        } else {
            return new ResponseResult(Code.FAIL, "添加失败！", null);
        }
    }

    @PostMapping("/delete")
    public ResponseResult updateCarType(@RequestBody G5CarType carType) {
        boolean flag = carTypeService.deleteCarType(carType.getId());
        if (flag) {
            return new ResponseResult(Code.SUCCESS, "删除成功！", null);
        } else {
            return new ResponseResult(Code.FAIL, "删除失败！", null);
        }
    }

}
